﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
        
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        
        SerialPort sPort1 = new SerialPort("COM1", 9600, Parity.None, 8, StopBits.One);

        private void pInit()
        {
        if ((System.IO.Ports.SerialPort.GetPortNames()).Length > 0)
            {
            for (int i=0; i<System.IO.Ports.SerialPort.GetPortNames().Length;i++)
                comboBox1.Items.Add(System.IO.Ports.SerialPort.GetPortNames()[i]);
            }
            comboBox1.SelectedIndex = 0;
        }

        private void pTxd()
        {
            sPort1.PortName = comboBox1.SelectedItem.ToString();
            try
            {
                sPort1.Open();
            }
            catch (SystemException)
            {
                label2.Text = "Failed. Error opening port!";
            }
            if (sPort1.IsOpen)
            {
                sPort1.Write(new byte[] { 0x7E, 0x00, 0x0C, 0x2B, 0x00, 0x10, 0x10, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00, 0x7E, 0x0C, 0x14, 0x3A, 0x7E }, 0, 22);
                sPort1.Close();
                label2.Text = "Unlock command sent successfully!";
            }
                           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pInit();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            pTxd();
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            sPort1.Dispose();
        }

    }
}
